#include "settingsdialog.h"
#include "ui_settingsdialog.h"

SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::SettingsDialog)
{
    m_ui->setupUi(this);
    this->setAttribute(Qt::WA_QuitOnClose, false);
    ServerChanged = false;
    connect(m_ui->checkBoxTrafficHint, SIGNAL(clicked(bool)), m_ui->comboBoxDirection, SLOT(setEnabled(bool)));
    connect(m_ui->lineEditServerAddress, SIGNAL(textChanged(QString)), this, SLOT(serverChanged()));
    connect(m_ui->spinBoxPort, SIGNAL(valueChanged(int)), this, SLOT(serverChanged()));
}

SettingsDialog::~SettingsDialog()
{
    delete m_ui;
}

void SettingsDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SettingsDialog::putSettings(const SETTINGS &settings)
{
    m_ui->lineEditServerAddress->setText(settings.serverIP);
    m_ui->spinBoxPort->setValue(settings.serverPort);
    m_ui->checkBoxAutoconnect->setChecked(settings.autoconn);
    m_ui->checkBoxReconnect->setChecked(settings.reconnect);
    m_ui->checkBoxSavePassword->setChecked(settings.savePass);
    m_ui->checkBoxMinimizeOnClose->setChecked(settings.minimizeOnClose);
    m_ui->checkBoxMoneyHint->setChecked(settings.outHintCash);
    m_ui->checkBoxTrafficHint->setChecked(settings.outHintTraffic);
    switch (settings.statType)
    {
        case ST_B:
            m_ui->radioButtonBytes->setChecked(true);
            break;
        case ST_KB:
            m_ui->radioButtonKilobytes->setChecked(true);
            break;
        case ST_MB:
            m_ui->radioButton_Megabytes->setChecked(true);
            break;
        default:
            m_ui->radioButtonFloating->setChecked(true);
            break;
    }
    m_ui->comboBoxDirection->clear();
    m_ui->comboBoxDirection->addItems(*settings.dirName);
    m_ui->comboBoxDirection->setCurrentIndex(settings.outHintTrafficDir);
    m_ui->comboBoxDirection->setEnabled(settings.outHintTraffic);
    m_ui->checkBoxSumm->setChecked(settings.showSumm);
}

void SettingsDialog::storeSettings(SETTINGS &settings)
{
    settings.serverIP = m_ui->lineEditServerAddress->text();
    settings.serverPort = m_ui->spinBoxPort->value();
    settings.autoconn = m_ui->checkBoxAutoconnect->isChecked();
    settings.reconnect = m_ui->checkBoxReconnect->isChecked();
    settings.savePass = m_ui->checkBoxSavePassword->isChecked();
    settings.minimizeOnClose = m_ui->checkBoxMinimizeOnClose->isChecked();
    settings.outHintCash = m_ui->checkBoxMoneyHint->isChecked();
    settings.outHintTraffic = m_ui->checkBoxTrafficHint->isChecked();
    settings.outHintTrafficDir = m_ui->comboBoxDirection->currentIndex();
    settings.showSumm = m_ui->checkBoxSumm->isChecked();

    if (m_ui->radioButtonBytes->isChecked())
    {
        settings.statType=ST_B;
    } else if (m_ui->radioButtonKilobytes->isChecked())
    {
        settings.statType=ST_KB;
    } else if (m_ui->radioButton_Megabytes->isChecked())
    {
        settings.statType=ST_MB;
    } else
    {
        settings.statType=ST_F;
    }
}

void SettingsDialog::serverChanged()
{
    ServerChanged = true;
}
